export default {
	data() {
		return {
			searchReq: {
				page: 1,
				limit: 10
			},
			totalPage: 1,
			refreshStatus: '',
			timer: null,
			_freshing: false,
			triggered: false //控制下拉加载后回弹回去
		}
	},
	methods: {
		// 单纯用于下拉 刷新
		fnRefresherrefresh() {
			console.log('--------下拉了');
			uni.showNavigationBarLoading()
			if (this._freshing) return;
			this._freshing = true;
			// 用于下拉加载中 三个点隐藏回去
			this.triggered = true;
			// 常用组件
			this.resetPage();
			try {
				this.getList(false);
			} catch (e) {
				uni.hideNavigationBarLoading()
			}
		},
		// 重置数据
		refreshData() {
			this.resetPage();
			this.triggered = true;
		},
		resetPage() {
			Object.assign(this.searchReq, {
				page: 1
			})
		},
		// totalCount 总条数  content 数据   status 是否加载更多
		setStatus(totalCount, content, status) {
			uni.hideNavigationBarLoading()
			this.totalPage = Math.ceil(totalCount/this.searchReq.limit);
			this._freshing = false;
			this.triggered = false
			console.log('==setStatus==>', new Date())
			// 上拉的状态： more 前；loading中；nomore没有更多了
			this.refreshStatus = this.searchReq.page >= this.totalPage ? 'nomore' : 'loadmore'
			if (status) {
				//新增
				this.listData = this.listData.concat(content);
			} else {
				//刷新
				uni.pageScrollTo({
					scrollTop: 0
				})
				uni.stopPullDownRefresh()
				this.listData = content;
			}
		},
		scrolltolower() {
			let newPage = this.searchReq.page + 1;
			console.log('==底部==>', this.totalPage, newPage)
			if (newPage > this.totalPage) return
			this.refreshStatus = 'loading';
			console.log('==scrolltolower==>', new Date())
			uni.showNavigationBarLoading()
			Object.assign(this.searchReq, {
				page: newPage
			})
			this.getList(true);
			setTimeout(function() {
				uni.hideNavigationBarLoading()
			}, 100);
		},
	},
	//下拉刷新
	onPullDownRefresh() {
		console.log('下拉刷新')
		clearTimeout(this.timer);
		this.timer = setTimeout(() => {
			this.triggered = true;
			this.resetPage();
			this.getList();
			// uni.stopPullDownRefresh();
		}, 100);
	},
	//上拉加载
	onReachBottom() {
		this.scrolltolower()
	}
}
